/**
 * 
 */
package com.android.ashu;








import com.android.cherryboard.R;
import com.android.cherryboard.Utility.Utility;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.PorterDuff.Mode;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
import android.widget.TextView;

/**
 * @author INVEERA
 *
 */
public class ProfileActivity extends BaseActivity implements ViewFactory {
	Integer pics[] = { R.drawable.img_thumb1, R.drawable.img_thumb2, R.drawable.img_thumb3, R.drawable.img_thumb4,
			R.drawable.img_thumb5 };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView();
		setUpView();

		//	setUpView1();


	}

	/*private void setUpView1() {
		ViewPager viewPager = (ViewPager)findViewById(R.id.viewpager);
		viewPager.setAdapter(new ViewPagerAdapter(this));

	}
	 */
	private void setContentView() {


		LinearLayout layoutAppContents = (LinearLayout)findViewById(R.id.layout_app_contents);
		View contentsView = ((LayoutInflater)ProfileActivity.this.getSystemService(ProfileActivity.this.LAYOUT_INFLATER_SERVICE))
				.inflate(R.layout.layout_profile_page, null);
		contentsView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		layoutAppContents.addView(contentsView);




	}
	/*
	 * 



		TextView tvUserName = (TextView) this
				.findViewById(R.id.textView_app_header_user_name);
		ImageView ivUserPic = (ImageView) this
				.findViewById(R.id.imageView_app_header_user_image);

		tvUserName.setText(Utility.userName);
		ivUserPic.setImageBitmap(Utility.getBitmap(Utility.userPicURL));

		ImageLoader imageLoader = new ImageLoader(this);
		imageLoader.DisplayImage(Utility.userPicURL, ivUserPic);

		/*ListView listViewCherryboard = (ListView) this
				.findViewById(R.id.listView_my_cherryboard);*/

	/*gridViewMyCherryboard = (GridView)this.findViewById(R.id.gridView_my_cherryboard);
		GetCherryboardListTask cherryboardListTask = new GetCherryboardListTask(
				gridViewMyCherryboard);
		String params = "http://cherryfull.com/app_services_data.php?tbl=tbl_app_cherryboard&tblFields=cherryboard_id,cherryboard_title&fb_id="+Utility.userUID;
		cherryboardListTask.execute(params);


		Button addCherryBoard = (Button)this.findViewById(R.id.button_add_cherryboard);
		addCherryBoard.setOnClickListener(this);*/








	private void setUpView() {
		TextView tvProfileName = (TextView)findViewById(R.id.txt_profile_name);
		TextView tvFollowers = (TextView)findViewById(R.id.txt_follower);
		TextView tvFollowing = (TextView)findViewById(R.id.txt_following);
		TextView tvExpertIn = (TextView)findViewById(R.id.txt_expert_in);
		TextView tvHelpWith = (TextView)findViewById(R.id.txt_help_with);
		ImageView imgProfile = (ImageView)findViewById(R.id.img_profie);

		tvFollowing.setText(getModifiedText("following", "121"));
		tvFollowers.setText(getModifiedText("followers", "168"));
		tvExpertIn.setText(getModifiedText("Expert in ", "healthy, eating, dancing, traveling"));
		tvHelpWith.setText(getModifiedText("Help with ", "family, career, frienships"));
		//Bitmap bm = getRoundedRectBitmap(BitmapFactory.decodeResource(ProfileActivity.this.getResources(), R.drawable.img_profile), 200);
		Bitmap bm = getRoundedRectBitmap(Utility.getBitmap(Utility.userPicURL),200);
		imgProfile.setImageBitmap(bm );
		imgProfile.setBackgroundResource(R.drawable.circle_shape_profile_image);
		tvProfileName.setText(Utility.userName);
		System.out.println("ID is "+Utility.userUID);

		// ids for the image swither and gallery
		/*Gallery imageGallery1= (Gallery)findViewById(R.id.Gallery01);
		Gallery imageGallery2= (Gallery)findViewById(R.id.Gallery02);
		final ImageSwitcher imageSwitcher1= (ImageSwitcher)findViewById(R.id.ImageSwitcher01);
		final ImageSwitcher imageSwitcher2= (ImageSwitcher)findViewById(R.id.ImageSwitcher02);
		imageSwitcher1.setFactory(ProfileActivity.this);
		imageSwitcher2.setFactory(ProfileActivity.this);
		setAnimation(imageSwitcher1);
		setAnimation(imageSwitcher2);

		imageGallery1.setAdapter(new ImageAdapter(this));
		imageGallery1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				imageSwitcher1.setImageResource(pics[arg2]);
			}
		});
		imageGallery2.setAdapter(new ImageAdapter(this));
		imageGallery2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				imageSwitcher2.setImageResource(pics[arg2]);
			}
		});

		 */

	}










	/*private void setAnimation(ImageSwitcher imageSwitcher1) {
		imageSwitcher1.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		imageSwitcher1.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));

	}*/

	private SpannableStringBuilder getModifiedText(String firstString,String secondString) {
		int textColor= getResources().getColor(R.color.expert_in_content_color);
		SpannableStringBuilder stringBuilder = new SpannableStringBuilder(firstString + secondString);
		stringBuilder.setSpan(new StyleSpan(android.graphics.Typeface.NORMAL), 0, firstString.length(),
				Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		stringBuilder.setSpan(new ForegroundColorSpan(textColor), firstString.length(),
				firstString.length() + secondString.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		return stringBuilder;


	}
	public Bitmap getRoundedRectBitmap(Bitmap bitmap, int pixels) {
		Bitmap result = null;
		try {
			result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
					Bitmap.Config.ARGB_8888);
			Canvas canvas = new Canvas(result);

			int color = 0xff424242;
			Paint paint = new Paint();
			Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
			RectF rectF = new RectF(rect);
			int roundPx = pixels;

			paint.setAntiAlias(true);
			canvas.drawARGB(0, 0, 0, 0);
			paint.setColor(color);
			canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

			paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
			canvas.drawBitmap(bitmap, rect, rect, paint);
		} catch (NullPointerException e) {
			// return bitmap;
		} catch (OutOfMemoryError o) {
		}
		return result;
	}


	public class ImageAdapter extends BaseAdapter {

		private Context ctx;

		public ImageAdapter(Context c) {
			ctx = c; 
		}

		@Override
		public int getCount() {

			return pics.length;
		}

		@Override
		public Object getItem(int arg0) {

			return arg0;
		}

		@Override
		public long getItemId(int arg0) {

			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {

			ImageView iView = new ImageView(ctx);
			iView.setImageResource(pics[arg0]);
			iView.setScaleType(ImageView.ScaleType.FIT_XY);
			iView.setLayoutParams(new Gallery.LayoutParams(200/3, android.widget.LinearLayout.LayoutParams.FILL_PARENT));
			return iView;
		}


	}




	@Override
	public View makeView() {
		ImageView iView = new ImageView(this);
		iView.setScaleType(ImageView.ScaleType.FIT_XY);
		iView.setLayoutParams(new 
				ImageSwitcher.LayoutParams(
						LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		iView.setBackgroundColor(0xFF000000);
		return iView;
	}

}
